<?php

namespace app\admin\controller;

use app\admin\model\Order as OrderModel;
use app\admin\model\OrderLog as OrderLogModel;
use app\common\controller\Backend;
use think\Session;

/**
 * 统计管理，没有实际 model，只做聚合
 *
 * @icon fa fa-circle-o
 */
class Analyze extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $admin_id = Session::get('admin')['id'];

        // 当前的时间戳
        $currentTimestamp = strtotime(date('Y-m-d H:i:00', time())) - 60;
        // 10 分钟前的时间戳
        $timestamp10MinutesAgo = $currentTimestamp - (10 * 60);

        if ($admin_id === 1) {
            $list = OrderModel
                ::with(['admin'])
                ->where('whatsup_status', 2)
                ->field('data_source_id,admin_id,country_ids,material_id,whatsup_url,sum(target_num) as total_target_num,sum(click_num) as total_click_num,sum(complete_num) as total_complete_num')
                ->group('data_source_id,admin_id')
                ->select();
        } else {
            $list = OrderModel
                ::with(['admin'])
                ->where('admin_id', $admin_id)
                ->where('whatsup_status', 2)
                ->field('data_source_id,admin_id,country_ids,material_id,whatsup_url,sum(target_num) as total_target_num,sum(click_num) as total_click_num,sum(complete_num) as total_complete_num')
                ->group('data_source_id,admin_id')
                ->select();
        }


        foreach ($list as $k => $v) {
            $list2 = OrderLogModel::where('data_source_id', $v['data_source_id'])
                ->where('admin_id', $v['admin_id'])
                ->where('createtime', '<', $currentTimestamp + 60)
                ->field('sum(click_num) as total_click_num,sum(complete_num) as total_complete_num')
                ->find();

            $list3 = OrderLogModel::where('data_source_id', $v['data_source_id'])
                ->where('admin_id', $v['admin_id'])
                ->where('createtime', '<', $timestamp10MinutesAgo + 60)
                ->field('sum(click_num) as total_click_num,sum(complete_num) as total_complete_num')
                ->find();

            $click_num_10 = 0;
            $complete_num_10 = 0;

            if ($list2 && $list3) {
                $click_num_10 = $list2['total_click_num'] - $list3['total_click_num'];
                $complete_num_10 = $list2['total_complete_num'] - $list3['total_complete_num'];
            }

            $list[$k]['click_num_10'] = $click_num_10;
            $list[$k]['complete_num_10'] = $complete_num_10;


            $list[$k]['conversion_rate'] = $v['total_complete_num'] > 0 ? $v['total_click_num'] / $v['total_complete_num'] : 0;


            $admin_total_num = OrderModel
                ::where('admin_id', $v['admin_id'])
                ->where('data_source_id', $v['data_source_id'])
                ->count();

            $admin_total_active_num = OrderModel
                ::where('admin_id', $v['admin_id'])
                ->where('data_source_id', $v['data_source_id'])
                ->where('whatsup_status', 2)
                ->count();


            $list[$k]['r_1'] = $admin_total_active_num . '/' . $admin_total_num;
            $extra_target_num = $v['total_target_num'] - $v['total_complete_num'];
            $list[$k]['extra_target_num'] = $extra_target_num;
            $list[$k]['r_2'] = $v['total_complete_num'] . '/' . $v['total_target_num'];

            $list[$k]['complete_time'] = '- -';
            if ($complete_num_10 > 0) {
                $need_minutes = round($extra_target_num / $complete_num_10 * 10, 1);
                $list[$k]['complete_time'] = date('Y-m-d H:i:s', time() + $need_minutes * 60);
            }
        }

        $result = ['total' => 1, 'rows' => $list];
        return json($result);
    }
}
